/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vd.wmt.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.FileDialog;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vd.wmt.interactive.BaseIntf;
import com.vd.wmt.interactive.SendMailOprIntf;
import com.vd.wmt.interactive.WageListAware;
import com.vd.wmt.model.WageInfo;
import com.vd.wmt.model.WageTableModel;
import com.vd.wmt.res.WageExcel;
import com.vd.wmt.util.MailSender;

/**
 *
 * @author Administrator
 */
public class ToolUI extends javax.swing.JFrame
implements BaseIntf,WageListAware,SendMailOprIntf
{
	private Log log = LogFactory.getLog(this.getClass());
	private List<? extends WageInfo> wageList;
	public List<? extends WageInfo> getWageList() {
		return wageList;
	}
	private WageTableModel wtModel = new WageTableModel();
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
     * Creates new form ToolUI
     */
    public ToolUI() {
        initComponents();
        initConfig();
        this.setExtendedState( MAXIMIZED_BOTH );
    }
    private void initConfig(){
        this.jXDatePickerWageMonth.setFormats("yyyy-MM");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        this.jXDatePickerWageMonth.setDate(cal.getTime());
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    private void initComponents() {

        jPanelConfig = new javax.swing.JPanel();
        jLabelPwd = new javax.swing.JLabel();
        jLabelWageMonth = new javax.swing.JLabel();
        jTextFieldUserId = new javax.swing.JTextField();
        jPasswordFieldPwd = new javax.swing.JPasswordField();
        jLabelUserId = new javax.swing.JLabel();
        jXDatePickerWageMonth = new org.jdesktop.swingx.JXDatePicker();
        jButtonLoadExcel = new javax.swing.JButton();
        jButtonSearchMail = new javax.swing.JButton();
        jButtonSendMail = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelPwd.setText("密  码：");

        jLabelWageMonth.setText("工资月份：");

        jTextFieldUserId.setText("");

        jPasswordFieldPwd.setText("");

        jLabelUserId.setText("用户名：");

        jButtonLoadExcel.setText("导入Excel");
        jButtonLoadExcel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoadExcelActionPerformed(evt);
            }
        });

        jButtonSearchMail.setText("查找邮箱");
        

        jButtonSendMail.setText("发送邮件");
        jButtonSendMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSendMailActionPerformed(evt);
            }
        });

        jTable1.setModel(wtModel);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanelConfigLayout = new javax.swing.GroupLayout(jPanelConfig);
        jPanelConfig.setLayout(jPanelConfigLayout);
        jPanelConfigLayout.setHorizontalGroup(
            jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelConfigLayout.createSequentialGroup()
                .addGroup(jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelConfigLayout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabelUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelConfigLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabelPwd, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPasswordFieldPwd, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(40, 40, 40)
                .addGroup(jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelConfigLayout.createSequentialGroup()
                        .addComponent(jLabelWageMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47)
                        .addComponent(jXDatePickerWageMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelConfigLayout.createSequentialGroup()
                        .addComponent(jButtonLoadExcel)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonSearchMail)
                        .addGap(30, 30, 30)
                        .addComponent(jButtonSendMail)))
                .addContainerGap(91, Short.MAX_VALUE))
            .addComponent(jScrollPane1)
        );
        jPanelConfigLayout.setVerticalGroup(
            jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelConfigLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelWageMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jXDatePickerWageMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelConfigLayout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelConfigLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jPasswordFieldPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelPwd, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelConfigLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonLoadExcel)
                            .addComponent(jButtonSearchMail)
                            .addComponent(jButtonSendMail))))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 800, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelConfig, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelConfig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonLoadExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoadExcelActionPerformed
    	FileDialog fileDialog = new FileDialog(this,"工资表excel");
    	fileDialog.setMode(FileDialog.LOAD);
    	fileDialog.setVisible(true);
    	//String file = fileDialog.getDirectory()+File.separator+fileDialog.getFile();
    	String file = fileDialog.getFile();
    	log.info("get file:"+file);
    	if(file == null || file.isEmpty()){
    		this.showErrMsg("未找到excel文件，请确认路径是否正确!");
    		return;
    	}
    	file = fileDialog.getDirectory()+File.separator+file;
    	WageExcel we = new WageExcel(file,this);
    	try {
			we.setWageList();
		} catch (Exception e) {
			e.printStackTrace();
			showErrMsg(e.getMessage());
		}
    	
    	
    }//GEN-LAST:event_jButtonLoadExcelActionPerformed

    private void jButtonSendMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchMailActionPerformed
    	MailSender sender = new MailSender(this);
    	sender.start();
    	
    }//GEN-LAST:event_jButtonSearchMailActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ToolUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ToolUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ToolUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ToolUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ToolUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonLoadExcel;
    private javax.swing.JButton jButtonSearchMail;
    private javax.swing.JButton jButtonSendMail;
    private javax.swing.JLabel jLabelPwd;
    private javax.swing.JLabel jLabelUserId;
    private javax.swing.JLabel jLabelWageMonth;
    private javax.swing.JPanel jPanelConfig;
    private javax.swing.JPasswordField jPasswordFieldPwd;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldUserId;
    private org.jdesktop.swingx.JXDatePicker jXDatePickerWageMonth;
    // End of variables declaration//GEN-END:variables
	@Override
	public void showErrMsg(String msg) {
		JOptionPane.showMessageDialog(this, "系统错误：\n"+msg);
	}
	@Override
	public void showTipMsg(String msg) {
		JOptionPane.showMessageDialog(this, msg);
	}
	@Override
	public void showWarnMsg(String msg) {
		
	}
	@Override
	public void setWageList(List<? extends WageInfo> list) {
		wageList = list;
		wtModel.setWageList(wageList);
	}
	@Override
	public String getPassword() {
		return this.jPasswordFieldPwd.getText();
	}
	@Override
	public String getUserName() {
		return this.jTextFieldUserId.getText();
	}
	@Override
	public String getWageMonth() {
		return new SimpleDateFormat("yyyy年MM月").format(this.jXDatePickerWageMonth.getDate());
	}
	@Override
	public void updateSendSts(int trow, String status) {
		this.wtModel.setValueAt(status, trow, this.wtModel.getColumnCount()-1);
		if(status.equals("FALID")){
			render.addRow(trow);
		}
		jTable1.getColumn(jTable1.getColumnName(this.wtModel.getColumnCount()-1)).setCellRenderer(render);
	}
	private MyTableCellRenderer render = new MyTableCellRenderer();
}
class MyTableCellRenderer extends DefaultTableCellRenderer{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public MyTableCellRenderer() {
	}
	private List<Integer> list = new ArrayList<Integer>();
	@Override
	public Component getTableCellRendererComponent(JTable table,
			Object value, boolean isSelected, boolean hasFocus,
			int row, int column) {
		Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		if(list.contains(row)){
			comp.setBackground(Color.RED);
		}else{
			comp.setBackground(Color.WHITE);
		}
        return comp;
	}
	public void addRow(int row){
		this.list.add(row);
	}
}
